package com.blb.controller;

import com.blb.entity.*;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.ITransferService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/transfer")
public class TransferController {

    @Resource
    private ITransferService service;

    /*
     * 作者：钟宇轩
     * 用途：查询所有移库单号
     * */
    @RequiresPermissions("transfer")
    @GetMapping("all")
    public String move(Model model){
        return("warehouse/transfer");
    }
    /*
     * 作者：钟宇轩
     * 用途：开启分页
     * */
    @ResponseBody
    @PostMapping("list")
    public BlbPage list(int start, int length){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<Transfer> list = service.findAll(start, length,companyId);
        int count = service.count();
        return new DataTablePage(count,count,list);
    }

    /*
     * 作者：钟宇轩
     * 用途：查询所有仓库
     * */
    @ResponseBody
    @GetMapping("allware")
    public ResponseHelp allWare(){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<InventoryCheck> allWare = service.findAllWare(companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allWare);
        return responseHelp;
    }

    /*
     * 作者：钟宇轩
     * 用途：条件查询所有要移库的商品
     * */
    @ResponseBody
    @GetMapping("search")
    public ResponseHelp search(String id, String iomGoods){
        List<IomWarehouse> search = service.search(id, iomGoods);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(search);
        return responseHelp;
    }

    /*
    * 作者：钟宇轩
    * 用途：通过仓库找到对应的货区
    * */
    @ResponseBody
    @GetMapping("findArea")
    public ResponseHelp findArea(String ware){
        String id = service.findWareId(ware);
        List<Wasc> allArea = service.findAllArea(id);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allArea);
        return responseHelp;
    }

    /*
    * 作者：钟宇轩
    * 用途：通过货区找到货架
    * */

    @ResponseBody
    @GetMapping("findShelves")
    public ResponseHelp findShelves(int id){
        ResponseHelp responseHelp = new ResponseHelp();
        List<Wasc> allShelves = service.findAllShelves(id);
        responseHelp.setData(allShelves);
        return responseHelp;
    }

    /*
    * 作者：钟宇轩
    * 用途：通过货架找到所有货位
    * */
    @ResponseBody
    @GetMapping("findCargo")
    public ResponseHelp findCargo(String id){
        List<Wasc> allCargo = service.findAllCargo(id);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allCargo);
        return responseHelp;
    }

    /*
    * 作者：钟宇轩
    * 用途：提交按钮后的修改、增加操作
    * */
    @ResponseBody
    @PostMapping("submission")
    public String submission(MoveDetailList moveDetails,IomDetailList iomDetails, String person, String tel, String time,int number,String reason,String outwarehouse,String inwarehouse,String production){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        service.submission(moveDetails,iomDetails,person,tel,time,number,reason,outwarehouse,inwarehouse,production,companyId);
        return "shit";
    }

    @ResponseBody
    @GetMapping("allDetails")
    public ResponseHelp details(String batch){
        Transfer transfer = service.moveRecord(batch);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(transfer);
        return responseHelp;
    }

    @ResponseBody
    @GetMapping("outWare")
    public ResponseHelp outWare(String id, String outware){
        List<IomWarehouse> iomWarehouses = service.outWare(id, outware);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(iomWarehouses);
        return responseHelp;
    }

}
